// Copyright (C) 2012 Denis Shienkov <denis.shienkov@gmail.com>
// Copyright (C) 2012 Laszlo Papp <lpapp@kde.org>
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause

#include "settingsdialog.h"
#include "ui_settingsdialog.h"

#include <QIntValidator>
#include <QLineEdit>
#include <QSerialPortInfo>

static const char blankString[] = QT_TRANSLATE_NOOP("SettingsDialog", "N/A");

SettingsDialog::SettingsDialog(QWidget *parent) :
	QDialog(parent),
	m_ui(new Ui::SettingsDialog),
	m_intValidator(new QIntValidator(0, 4000000, this))
{
	m_ui->setupUi(this);

	m_ui->baudRateBox->setInsertPolicy(QComboBox::NoInsert);

	connect(m_ui->applyButton, &QPushButton::clicked,
			this, &SettingsDialog::apply);
	connect(m_ui->serialPortInfoListBox, &QComboBox::currentIndexChanged,
			this, &SettingsDialog::showPortInfo);
	fillPortsParameters();
	fillPortsInfo();

	updateSettings();
}

SettingsDialog::~SettingsDialog()
{
	delete m_ui;
}

SettingsDialog::Settings & SettingsDialog::settings()
{
	return m_currentSettings;
}

void SettingsDialog::showPortInfo(int idx)
{
	if (idx == -1)
		return;

	const QString blankString = tr(::blankString);

	const QStringList list = m_ui->serialPortInfoListBox->itemData(idx).toStringList();
	m_ui->descriptionLabel->setText(tr("Description: %1").arg(list.value(1, blankString)));
	m_ui->manufacturerLabel->setText(tr("Manufacturer: %1").arg(list.value(2, blankString)));
	m_ui->serialNumberLabel->setText(tr("Serial number: %1").arg(list.value(3, blankString)));
	m_ui->locationLabel->setText(tr("Location: %1").arg(list.value(4, blankString)));
	m_ui->vidLabel->setText(tr("Vendor Identifier: %1").arg(list.value(5, blankString)));
	m_ui->pidLabel->setText(tr("Product Identifier: %1").arg(list.value(6, blankString)));
}

void SettingsDialog::apply()
{
	updateSettings();
	hide();
}

void SettingsDialog::fillPortsParameters()
{
	m_ui->baudRateBox->addItem(QStringLiteral("1200"), QSerialPort::Baud1200);
	m_ui->baudRateBox->addItem(QStringLiteral("2400"), QSerialPort::Baud2400);
	m_ui->baudRateBox->addItem(QStringLiteral("4800"), QSerialPort::Baud4800);
	m_ui->baudRateBox->addItem(QStringLiteral("9600"), QSerialPort::Baud9600);
	m_ui->baudRateBox->addItem(QStringLiteral("19200"), QSerialPort::Baud19200);
	m_ui->baudRateBox->addItem(QStringLiteral("38400"), QSerialPort::Baud38400);
	m_ui->baudRateBox->addItem(QStringLiteral("57600"), QSerialPort::Baud57600);
	m_ui->baudRateBox->addItem(QStringLiteral("115200"), QSerialPort::Baud115200);
	m_ui->baudRateBox->setCurrentIndex(3);

	m_ui->dataBitsBox->addItem(QStringLiteral("5"), QSerialPort::Data5);
	m_ui->dataBitsBox->addItem(QStringLiteral("6"), QSerialPort::Data6);
	m_ui->dataBitsBox->addItem(QStringLiteral("7"), QSerialPort::Data7);
	m_ui->dataBitsBox->addItem(QStringLiteral("8"), QSerialPort::Data8);
	m_ui->dataBitsBox->setCurrentIndex(3);

	m_ui->parityBox->addItem(tr("None"), QSerialPort::NoParity);
	m_ui->parityBox->addItem(tr("Even"), QSerialPort::EvenParity);
	m_ui->parityBox->addItem(tr("Odd"), QSerialPort::OddParity);
	m_ui->parityBox->addItem(tr("Mark"), QSerialPort::MarkParity);
	m_ui->parityBox->addItem(tr("Space"), QSerialPort::SpaceParity);
	m_ui->parityBox->setCurrentIndex(0);

	m_ui->stopBitsBox->addItem(QStringLiteral("1"), QSerialPort::OneStop);
#ifdef Q_OS_WIN
	m_ui->stopBitsBox->addItem(tr("1.5"), QSerialPort::OneAndHalfStop);
#endif
	m_ui->stopBitsBox->addItem(QStringLiteral("2"), QSerialPort::TwoStop);
	m_ui->stopBitsBox->setCurrentIndex(0);

	m_ui->flowControlBox->addItem(tr("None"), QSerialPort::NoFlowControl);
	m_ui->flowControlBox->addItem(tr("RTS/CTS"), QSerialPort::HardwareControl);
	m_ui->flowControlBox->addItem(tr("XON/XOFF"), QSerialPort::SoftwareControl);
	m_ui->flowControlBox->setCurrentIndex(0);
}

void SettingsDialog::fillPortsInfo()
{
	m_ui->serialPortInfoListBox->clear();
	const QString blankString = tr(::blankString);
	const auto infos = QSerialPortInfo::availablePorts();

	for (const QSerialPortInfo &info : infos) {
		QStringList list;
		const QString description = info.description();
		const QString manufacturer = info.manufacturer();
		const QString serialNumber = info.serialNumber();
		const auto vendorId = info.vendorIdentifier();
		const auto productId = info.productIdentifier();
		list << info.portName()
			 << (!description.isEmpty() ? description : blankString)
			 << (!manufacturer.isEmpty() ? manufacturer : blankString)
			 << (!serialNumber.isEmpty() ? serialNumber : blankString)
			 << info.systemLocation()
			 << (vendorId ? QString::number(vendorId, 16) : blankString)
			 << (productId ? QString::number(productId, 16) : blankString);

		m_ui->serialPortInfoListBox->addItem(list.constFirst(), list);
	}

	m_ui->serialPortInfoListBox->addItem(tr("Custom"));
}

void SettingsDialog::updateSettings()
{
	m_currentSettings.name = m_ui->serialPortInfoListBox->currentText();

	const auto baudRateData = m_ui->baudRateBox->currentData();
	m_currentSettings.baudRate = baudRateData.value<QSerialPort::BaudRate>();
	m_currentSettings.stringBaudRate = QString::number(m_currentSettings.baudRate);

	const auto dataBitsData = m_ui->dataBitsBox->currentData();
	m_currentSettings.dataBits = dataBitsData.value<QSerialPort::DataBits>();
	m_currentSettings.stringDataBits = m_ui->dataBitsBox->currentText();

	const auto parityData = m_ui->parityBox->currentData();
	m_currentSettings.parity = parityData.value<QSerialPort::Parity>();
	m_currentSettings.stringParity = m_ui->parityBox->currentText();

	const auto stopBitsData = m_ui->stopBitsBox->currentData();
	m_currentSettings.stopBits = stopBitsData.value<QSerialPort::StopBits>();
	m_currentSettings.stringStopBits = m_ui->stopBitsBox->currentText();

	const auto flowControlData = m_ui->flowControlBox->currentData();
	m_currentSettings.flowControl = flowControlData.value<QSerialPort::FlowControl>();
	m_currentSettings.stringFlowControl = m_ui->flowControlBox->currentText();

	m_currentSettings.localEchoEnabled = m_ui->localEchoCheckBox->isChecked();
}

void SettingsDialog::fillStringsToUI()
{
	m_ui->serialPortInfoListBox->setCurrentText(m_currentSettings.name);
	if (m_ui->serialPortInfoListBox->currentIndex()<0 ||
			m_ui->serialPortInfoListBox->currentIndex()>=
			m_ui->serialPortInfoListBox->count())
		m_ui->serialPortInfoListBox->setCurrentIndex(0);

	m_ui->baudRateBox->setCurrentText(m_currentSettings.stringBaudRate);
	if (m_ui->baudRateBox->currentIndex()<0 ||
			m_ui->baudRateBox->currentIndex()>=
			m_ui->serialPortInfoListBox->count())
		m_ui->baudRateBox->setCurrentIndex(3);

	m_ui->dataBitsBox->setCurrentText(m_currentSettings.stringDataBits);
	if (m_ui->dataBitsBox->currentIndex()<0 || m_ui->dataBitsBox->currentIndex()
			>=m_ui->dataBitsBox->count())
		m_ui->dataBitsBox->setCurrentIndex(3);


	m_ui->parityBox->setCurrentText(m_currentSettings.stringParity);
	if (m_ui->parityBox->currentIndex()<0 || m_ui->parityBox->currentIndex()
			>=m_ui->parityBox->count())
		m_ui->parityBox->setCurrentIndex(0);

	m_ui->stopBitsBox->setCurrentText(m_currentSettings.stringStopBits);
	if (m_ui->stopBitsBox->currentIndex()<0 || m_ui->stopBitsBox->currentIndex()
			>=m_ui->stopBitsBox->count())
		m_ui->stopBitsBox->setCurrentIndex(0);

	m_ui->flowControlBox->setCurrentText(m_currentSettings.stringFlowControl);
	if (m_ui->flowControlBox->currentIndex()<0 || m_ui->flowControlBox->currentIndex()
			>=m_ui->flowControlBox->count())
		m_ui->flowControlBox->setCurrentIndex(0);

	m_ui->localEchoCheckBox->setChecked(m_currentSettings.localEchoEnabled);
}
